import enum
from enum import IntEnum


class BaseIntEnum(IntEnum):

    @classmethod
    def valid_enums(cls):
        return [i.value for i in cls]


class RegisterTypeEnum:
    CONFIG = 'config'
    CONTROL = 'control'
    DATA = 'data'


class ErrorCodeEnum(BaseIntEnum):

    """
    0x00正确  0x01升级包错误 需要重传  0x02参数错误    0x80 0点偏移   0x81 0点满偏
    """
    OK = 0
    PACKAGE_ERROR = 1
    PARAM_ERROR = 2
    OFFSET_ZERO = 128
    FULL_ZERO = 129


class CommandEnum(IntEnum):
    """
    0x00建立通信    0x01保持通信    0x02写寄存器    0x03读寄存器
    0x04数据上传    0x05触发IAP   0x06发送Bin文件     0x07发送Bin文件结束
    """

    CREATE = 0
    KEEP = 1
    WRITE = 2
    READ = 3
    UPLOAD = 4
    IAP = 5
    BIN_START = 6
    BIN_END = 7


class RegisterEnum(BaseIntEnum):
    """
    0x10传感器本机IP地址   0x11传感器目的IP地址   0x12量程
    0x13SN号设置   0x14传感器读取频率     0x15解耦系数
    """

    SELF = 16
    DEST = 17
    MEASUREMENT = 18
    SN = 19
    HZ = 20
    DECOUPLING = 21


class ControlRegisterEnum(BaseIntEnum):
    """
    0x30启动数据采集  0x31停止数据采集   0x32取零
    """

    START = 48
    END = 49
    ZERO = 50


class DataRegisterEnum(BaseIntEnum):
    """
    0x40基础坐标系力值X    0x41基础坐标系力值Y    0x42基础坐标系力值Z    0x43基础坐标系力矩X    0x44基础坐标系力矩Y    0x45基础坐标系力矩Z
    0x46基础坐标系下的水平合力     0x47基础坐标系下的合力   0x48基础坐标系下的合力矩  0x49 1Hz低通滤波后的基础坐标系力值X
    0x4A 1Hz低通滤波后的基础坐标系力值Y  0x4B 1Hz低通滤波后的基础坐标系力值Z  0x4C 1Hz低通滤波后的基础坐标系力矩X  0x4D 1Hz低通滤波后的基础坐标系力矩Y
    0x4E 1Hz低通滤波后的基础坐标系力矩Z  0x4F 1Hz低通滤波后的基础坐标系下的水平合力   0x50 1Hz低通滤波后的基础坐标系下的合力 0x51 1Hz低通滤波后的基础坐标系下的合力矩
    """
    pass


class OperationCommandEnum(BaseIntEnum):

    KEEP_ALIVE = 0


class OperationEnum(BaseIntEnum):

    IP_CONFIG = 1
    DATA_COLLECT = 2
    LIVE_HREF = 3
    SN = 4
    MEASUREMENT = 5
    HZ = 6
    EXIT = 7


class CollectEnum(BaseIntEnum):

    START = 1
    END = 2
    TAKE_ZERO = 3
    # EXIT = 4

    @classmethod
    def get_operation_msg(cls, cat):
        if cat == cls.START:
            return ControlRegisterEnum.START
        elif cat == cls.END:
            return ControlRegisterEnum.END
        elif cat == cls.TAKE_ZERO:
            return ControlRegisterEnum.ZERO

    @classmethod
    def get_collect_desc(cls, collect):
        if collect == -1:
            return '初始化'
        elif collect == cls.START:
            return '采集中'
        elif collect == cls.END:
            return '暂停中'
        elif collect == cls.TAKE_ZERO:
            return '取零'


class AllowRegisterTypeEnum(enum.Enum):

    IP = 'ip'
    SN = 'sn'
    HZ = 'hz'
    MEASUREMENT = 'measurement'
    DECOUPLING = 'decoupling'
    COLLECT = 'collect'

    @classmethod
    def valid_enums(cls):
        return [i.value for i in cls]
